<script lang="ts" setup>
import { watch, ref } from 'vue'
import axios from 'axios'
import { TResponse, TArticleItem } from '../types';
import useToutiaoStore from '../store'
const toutiaoStore = useToutiaoStore()
const articleList = ref<TArticleItem[]>([])

// !#3
watch(() => toutiaoStore.channelId, async (newChannelId: number) => {
  const r = await axios.get<TResponse<{ results: TArticleItem[] }>>(`http://geek.itheima.net/v1_0/articles?channel_id=${newChannelId}&timestamp=${Date.now()}`)
  articleList.value = r.data.data.results
})
</script>

<template>
  <div className="list">
    <div className="article_item" v-for="item in articleList" :key="item.art_id">
      <h3 className="van-ellipsis">{{item.title}}</h3>
      <div className="img_box" v-if="item.cover.type === 1">
        <img :src="item.cover.images![0]" className="w100" alt="" />
      </div>
      <!-- <div className="img_box" v-if="item.cover.images">
        <img :src="item.cover.images[0]" className="w100" alt="" />
      </div> -->
      <div className="info_box">
        <span>{{item.aut_name}}</span>
        <span>{{item.comm_count}}评论</span>
        <span>{{item.pubdate}}</span>
      </div>
    </div>
  </div>
</template>

<style scoed lang="less"></style>